package com.jmb.apiplatform.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jmb.apiplatform.common.PageResult;
import com.jmb.apiplatform.exception.ValidException;
import com.jmb.apiplatform.mapper.ProjectMapper;
import com.jmb.apiplatform.pojo.ProjectDao;
import com.jmb.apiplatform.pojo.ProjectVO;
import com.jmb.apiplatform.service.ProjectService;
import com.jmb.apiplatform.util.ValidUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import java.util.Date;
import java.util.List;


@Slf4j
@Service
@EnableTransactionManagement
public class ProjectServiceImpl  implements ProjectService {
    @Autowired
    private ProjectMapper projectMapper;

    @Override
    public void saveProject(ProjectDao projectDao) throws ValidException {
        ValidUtil.notNUll(projectDao, "项目信息不能为空");
        ValidUtil.notNUll(projectDao.getName(), "项目名称不能为空");
        ValidUtil.notNUll(projectDao.getProtocol(),"调试环境协议不能为空");
        ValidUtil.notNUll(projectDao.getDomain(),"调试环境域名不能为空");
        ValidUtil.notNUll(projectDao.getDevProtocol(),"开发环境协议不能为空");
        ValidUtil.notNUll(projectDao.getDevDomain(),"开发环境域名不能为空");
        ValidUtil.notNUll(projectDao.getTestProtocol(),"测试环境协议不能为空");
        ValidUtil.notNUll(projectDao.getTestDomain(),"测试环境域名不能为空");
        ValidUtil.notNUll(projectDao.getStgProtocol(),"预发布环境协议不能为空");
        ValidUtil.notNUll(projectDao.getStgDomain(),"预发布环境域名不能为空");
        ValidUtil.notNUll(projectDao.getProdProtocol(),"生产环境域名不能为空");
        ValidUtil.notNUll(projectDao.getProdDomain(),"生产环境域名不能为空");
        projectDao.setCreatedTime(new Date());
        projectDao.setUpdateTime(new Date());
        projectMapper.saveProject(projectDao);
    }

    @Override
    public PageResult<ProjectDao> listProject(ProjectVO projectVO) {
        PageHelper.startPage(projectVO.getPageNum(), projectVO.getPageSize());
        List<ProjectDao> pageList = projectMapper.pageList(projectVO);
        Page<ProjectDao> p=(Page<ProjectDao>)pageList;
        return new PageResult<>(p.getTotal(),p.getResult());
    }

    @Override
    public void removeById(Integer id) throws ValidException {
        ValidUtil.notNUll(id, "项目id不能为空");
        int proId = projectMapper.deleteProjectId(id);
        if (proId <= 0){
            throw new ValidException("数据不存在，请检查重试");
        }
    }

    @Override
    public void updateProject(ProjectDao projectDao) throws ValidException {
        ValidUtil.notNUll(projectDao, "项目信息不能为空");
        projectDao.setUpdateTime(new Date());
        projectMapper.updateProject(projectDao);
    }

    @Override
    public ProjectDao finProjectId(Integer id) throws ValidException {
        if (id==null){
            throw new ValidException("项目id不能为空");
        }
        ProjectDao projectDao = projectMapper.finProjectId(id);
        if (projectDao ==null){
            throw new ValidException("项目不存在");
        }
        return projectDao;

    }

    @Override
    public String findProjectName(String name) throws ValidException {
        ProjectDao projectDao = projectMapper.finProjectName(name);
        if (projectDao.getName()==null){
            throw new ValidException("项目名称不存在");
        }
        return projectDao.getName();
    }

    @Override
    public ProjectDao findProject(ProjectDao projectDao) {
        return projectMapper.selectProject(projectDao);
    }

    @Override
    public List<ProjectDao> projectList() {
        List<ProjectDao> projectDaos = projectMapper.ListProject();
        return projectDaos;
    }

    @Override
    public ProjectVO findProjectById(Integer projectId) {
        return projectMapper.selectProjectById(projectId);
    }


}




